package com.project.class8_23;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallTest {
    public static void main(String[] args) {
        //创建FutureTask对象，该类为Runnable实现类。
        //传入Callable实现类对象，线程启动时，会执行Callable实现类的call方法
        FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for(int i=1;i<=50;i++){
                    sum += i;
                }

                return sum;
            }
        });
        //启动线程
        new Thread(task).start();
        try {
            //得到线程执行完毕后的返回值
            Integer sum = task.get();
            System.out.println(sum);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
